package com.building.hcrm;

import java.util.ArrayList;
import java.util.EventListener;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.building.hcrm.common.CrashHandler;

import android.app.Activity;
import android.app.Application;

public class HcrmApplication extends Application
{
	ArrayList<Activity> list = new ArrayList<Activity>();
	public LocationClient mLocationClient;
	public BDLocationListener mMyLocationListener;

	private BaiduLocationListener _locationListener;

	public interface BaiduLocationListener extends EventListener
	{
		void onBaiduLocation(BDLocation location);
	}

	public void setOnBaiduLocationListener(BaiduLocationListener listener)
	{
		_locationListener = listener;
	}

	private void setOnBaiduLocation(BDLocation location)
	{
		if (_locationListener != null)
		{
			_locationListener.onBaiduLocation(location);
		}
	}

	@Override
	public void onCreate()
	{
		super.onCreate();
		CrashHandler crashHandler = CrashHandler.getInstance();
		crashHandler.init(this);
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener); // 注册监听函数
	}

	/**
	 * 实现实位回调监听
	 */
	private class MyLocationListener implements BDLocationListener
	{

		@Override
		public void onReceiveLocation(BDLocation location)
		{
			// Receive Location
			setOnBaiduLocation(location);
		}
	}

	/**
	 * Activity关闭时，删除Activity列表中的Activity对象
	 */
	public void removeActivity(Activity a)
	{
		list.remove(a);
	}

	/**
	 * 向Activity列表中添加Activity对象
	 */
	public void addActivity(Activity a)
	{
		list.add(a);
	}

	/**
	 * 关闭Activity列表中的所有Activity
	 */
	public void finishActivity()
	{
		for (Activity activity : list)
		{
			if (null != activity)
			{
				activity.finish();
			}
		}
	}
}
